package cn.cvs.servlet.sysUser;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import cn.cvs.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpdatePasswordServlet", urlPatterns = "/user/savePassword")
public class UpdatePasswordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SysUserService sysUserService= (SysUserService) SpringUtil.getBean ( "sysUserService" );
        SysUser user = (SysUser) request.getSession ( ).getAttribute ( "userSession" );
        String newPassword = request.getParameter ( "newPassword" );
        user.setPassword ( newPassword );
        SysUser userSession = (SysUser) request.getSession ( ).getAttribute ( "userSession" );
        user.setUpdatedUserId ( userSession.getId () );
        int i = sysUserService.updateUser ( user );
        if (i>0){
            request.getSession ( ).invalidate ();
            request.getRequestDispatcher ( "/WEB-INF/jsp/login.jsp" ).forward ( request,response );
        }else {
            request.setAttribute ( "message","旧密码输入有误，请检查后重试！" );
            request.getRequestDispatcher ( "/sys/user/toUpdatePwd" ).forward ( request,response );
        }
    }
}
